from flask import Blueprint, jsonify, request
from db import db1
from utils.comm import get_data
import json

bp_cbf = Blueprint('cflask', __name__)


@bp_cbf.route('/yw', methods=['POST', 'GET'])
def yw():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = """insert into yw values (%d,'%s','%s')""" % (0, data['name'], '')
        db1.insert(sql)
        return jsonify({'code': 200})
    elif request.method == 'GET':
        sql = """select * from yw"""
        list = db1.find_all(sql)
        return jsonify({'code': 200, "list": list})


@bp_cbf.route('/setparams', methods=['POST', 'GET'])
def setparams():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update yw set canshu='%s' where id=%d" % (data['canshu'], int(data['id']))
        db1.insert(sql)
        return jsonify({"code": 200})
    elif request.method == 'GET':
        wid = request.args.get('wid')
        sql = """select * from yw where id=%d""" % (int(wid))
        res = db1.find_one(sql)
        return jsonify({'code': 200, "list": res})


@bp_cbf.route('/yhlM', methods=['POST', 'GET'])
def yhlM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = """insert into yhl values (%d,'%d','%d','%s')""" % (0, int(data['uid']), int(data['wid']), data['pamars'])
        db1.insert(sql)
        return jsonify({'code': 200})


###########################################################

def xtree(data):
    list = []
    tree = {}
    if not data:
        return data
    for i in data:
        tree[i['id']] = i
    for j in data:
        pid = j['pid']
        if pid == 0:
            list.append(j)
        else:
        # 查看父类下有没有子类
            if 'son' not in tree[pid]:
                # 如果没有初始化列表
                tree[pid]['son'] = []
            # 把子类加到父类列表中
            tree[pid]['son'].append(j)
    return list



@bp_cbf.route('/cateM', methods=['POST', 'GET'])
def cateM():
    if request.method == 'POST':
        data = get_data()
        sql = """insert into cates values(0,'%s',%d,%s)""" % (data['name'], int(data['pid']), data['params'])
        db1.insert(sql)
        return jsonify({'code': 200})

    elif request.method == 'GET':
        sql = """select * from  cates"""
        res = db1.find_all(sql)
        data = xtree(res)
        return jsonify({'code': 200, "list": data})

import time
@bp_cbf.route('/teaM',methods=['POST', 'GET'])
def teaM():
    if request.method == 'GET':
        time.sleep(0.1)
        sql = """select * from teacher"""
        res= db1.find_all(sql)
        return jsonify({"code":200,"list":res})

@bp_cbf.route('/getCateByid')
def getCateByid():
    id = request.args.get('id')
    sql = "select params from cates where id=%d" % (int(id))
    res = db1.find_one(sql)
    return jsonify({'code': 200, 'params': res['params']})


@bp_cbf.route('/courseM', methods=['POST', 'GET'])
def courseM():
    data = get_data()
    if data['params']:
        sql = "insert into courses values(0,'%s',%d,'%s','%s','%s',%d,%d)" % (
        data['title'], int(data['pid']), data['params'], data['pic'], data['descrip'], int(data['teacher_id']),
        int(data['status']))
    else:
        sql = "insert into courses(title,pid,pic,descrip,teacher_id,status) values('%s',%d,'%s','%s',%d,%d)" % (
        data['title'], int(data['pid']), data['pic'], data['descrip'], int(data['teacher_id']), int(data['status']))

    db1.insert(sql)
    return jsonify({"code": 200})